unit uFormEditor;

interface

  uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs, StdCtrls, ComCtrls, Menus;

  type
    TfrmEditor = class(TForm)
      memo1: TRichEdit;
      SaveDialog1: TSaveDialog;
      MainMenu1: TMainMenu;
      Arquivo1: TMenuItem;
      Salvar1: TMenuItem;
      Abrir1: TMenuItem;
      N1: TMenuItem;
      Fechar1: TMenuItem;
      StatusBar1: TStatusBar;
      Ferramentas1: TMenuItem;
      Formatar1: TMenuItem;
      procedure FormClose(Sender: TObject; var Action: TCloseAction);
      procedure Salvar1Click(Sender: TObject);
      procedure Fechar1Click(Sender: TObject);
      procedure FormCreate(Sender: TObject);
      procedure memo1Change(Sender: TObject);
      procedure Formatar1Click(Sender: TObject);
      private
        { Private declarations }
        palavras_chave: TStringList;
        procedure inicializar_variaveis();

      public
        { Public declarations }
        procedure formatar();
    end;

  var
    frmEditor: TfrmEditor;

  const
    NUMEROS = '0123456789';

implementation

  {$R *.dfm}

  procedure TfrmEditor.Fechar1Click(Sender: TObject);
    begin
      close();
    end;

  procedure TfrmEditor.formatar;
    var
      i, j: integer;

      function comparar(i: integer; keyWord: String): boolean;
        begin
          result := AnsiUpperCase(keyWord) = AnsiUpperCase
            (copy(memo1.Text, i - 1, length(keyWord)));
        end;

    begin
      try
        for i := 1 to length(memo1.Text) do
          for j := 0 to palavras_chave.Count - 1 do
            if comparar(i, palavras_chave.Names[j]) then
              begin
                memo1.SelStart := i - 1;
                memo1.SelLength := length(palavras_chave.Names[j]);
                memo1.SelAttributes.Color := StringToColor
                  (palavras_chave.Values[palavras_chave.Names[j]]);
                continue;
              end;
      except
        on e: Exception do
          begin
            showmessage(e.Message);
          end;
      end; // try
    end; // procedure

  procedure TfrmEditor.Formatar1Click(Sender: TObject);
    begin
      self.formatar();
    end;

  procedure TfrmEditor.FormClose(Sender: TObject; var Action: TCloseAction);
    begin
      self.Release();
    end;

  procedure TfrmEditor.FormCreate(Sender: TObject);
    begin
      inicializar_variaveis();
    end;

  procedure TfrmEditor.inicializar_variaveis;
    begin
      palavras_chave := TStringList.Create();
      with palavras_chave do
        begin
          add('if=clblue');
          add('then=clblue');
          add('begin=clBlue');
          add('end=clblue');
          add('record=clblue');
        end;
    end;

  procedure TfrmEditor.memo1Change(Sender: TObject);
    begin
      StatusBar1.Panels[0].Text := IntToStr(memo1.Lines.Count) + ' linha(s)';
    end;

  procedure TfrmEditor.Salvar1Click(Sender: TObject);
    var
      arq: textfile;
      i: integer;
    begin

      if SaveDialog1.Execute then
        begin
          // memo1.lines.SaveToFile(SaveDialog1.FileName);
          AssignFile(arq, SaveDialog1.FileName);
          if FileExists(SaveDialog1.FileName) then
            Reset(arq)
          else
            Rewrite(arq);
          for i := 0 to memo1.Lines.Count - 1 do
            Write(arq, memo1.Lines[i]);

          CloseFile(arq);
        end;
    end;

end.
